// @ts-nocheck
import './assets/main.css'
import './assets/vant.css'
import { createApp } from 'vue'
import { createPinia } from 'pinia'

import App from './App.vue'
import router from './router'
import {
    Dialog,
    Toast,
    Empty,
    Loading,
    Search,
    Tabbar, 
    TabbarItem
} from "vant"
import config from "./config"
import { getWechatUserInfo } from "@/api/common"
import "@/utils/tools"
import { showToast } from 'vant';

const app = createApp(App)

app.config.globalProperties.$showToast = showToast;

if (!window.openId) {
    window.openId = Request2("openid")
}

if (!window.openId) {
    let appId = config[
        import.meta.env.MODE].appId //正式
    let callbackUrl =
        config[
            import.meta.env.MODE].baseUrl + "/promoter/sendRedirect"
    let hash = window.location.hash;
    
    let callbackParam = {
        url: config[
            import.meta.env.MODE].h5Url + "/index.html",
        hash: hash.slice(2)
    }

    let params = getUrlParams();

    callbackParam = {...callbackParam, ...params}

    let replaceUrl =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
        appId +
        "&redirect_uri=" +
        callbackUrl +
        "?callBackJson=" +
        encodeURIComponent(JSON.stringify(callbackParam)) +
        "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"

        console.log(replaceUrl)
    window.location.href = replaceUrl
} else {
    window.userInfo = null
    router.beforeEach((to, from, next) => {
        if (to.meta.title) {
            document.title = to.meta.title
        }
        if (!window.userInfo) {
            getWechatUserInfo({
                    openid: openId,
                }).then((data) => {
                    window.userInfo = data
                    next()
                })
                .catch((data) => {
                    next()
                })
        } else {
            next()
        }

        // next()
    })
}


app.use(createPinia())
app.use(router)

app.use(router)
    .use(Toast)
    .use(Empty)
    .use(Dialog)
    .use(Loading)
    .use(Search)
    .use(Tabbar)
    .use(TabbarItem)
    

app.mount('#app')
